﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IDAL;
using IChessEngine;
using IBLL;
using ChsExceptions.GameEdit;

namespace BLL
{
    public abstract class ChsGameEditBLL : IChsGameEditBLL
    {
        protected IChsGameEditDAO dao = null;
        private Dictionary<Guid, IChsGame> games;
        public ChsGameEditBLL()
        {
            games = new Dictionary<Guid, IChsGame>();
        }
        public abstract void OpenDAO(params object[] parameters);
        public abstract void CloseDAO(params object[] parameters);
        public IChsGamePosition GetRootPosition(Guid gameId)
        {
            return games[gameId].GetRootPosition();
        }
        public IChsGameMovement AddMovement(Guid gameId, IChsGamePosition currentPosition, IChsMovement movement)
        {
            return games[gameId].AddMovement(currentPosition, movement);
        }
        public IChsGamePosition GetTargetPosition(Guid gameId, IChsGameMovement movement)
        {
            return games[gameId].GetTargetPosition(movement);
        }
        public IChsGamePosition GetTargetPosition(Guid gameId, Guid positionId)
        {
            return games[gameId].GetTargetPosition(positionId);
        }
        public IChsGamePosition FindBackwardPosition(Guid gameId, IChsGamePosition currentPosition)
        {
            return games[gameId].FindBackwardPosition(currentPosition);
        }
        public List<IChsGameMovement> FindForwardMovements(Guid gameId, IChsGamePosition currentPosition)
        {
            return games[gameId].FindForwardMovements(currentPosition);
        }
        public List<IChsGameInfo> GetGames()
        {
            return dao.GetGames();
        }
        public IChsGame OpenGame(Guid gameId)
        {
            if (games.ContainsKey(gameId))
                throw new ChsGameAllreadyOpenException();
            if (dao == null)
                throw new ChsNoDaoException();
            IChsGame returned = dao.GetGame(gameId);
            games.Add(gameId, returned);
            return returned;
        }
        public void CloseGame(Guid gameId)
        {
            throw new NotImplementedException();
        }
        public void Save()
        {
            if (dao == null)
                throw new ChsNoDaoException();
            dao.Save();
        }
    }
}
